<script>
    layui.use(['layer', 'table', 'form','laydate'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;

        var dataTable = table.render({
            elem: '#dataTable'
            , height: 'full-100'
            , url: "/admin/ad/list/data"
            , defaultToolbar: []
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , page: true
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width: 80}
                , {field: 'ad_title', title: '名称'}
                , {field: 'category_name', title: '分类'}
                , {field: 'thumb', title: '广告图片', templet: '#imgurl'}
                , {field: 'sort', title: '排序', templet: '#sorttpl'}
                , {field: 'start_time', title: '开始时间'}
                , {field: 'end_time', title: '结束时间' }
                , {field: 'status', title: '状态',templet:'#statustpl'}
                , {title: '操作', width: 150, align: 'center', toolbar: '#options'}
            ]]
        });
        //日期时间范围
        laydate.render({
            elem: '#test10'
            ,type: 'datetime'
            ,range: true,
            isclear: false, //是否显示清空
        });

        $('#search').click(function () {
            var ad_title = $('#ad_title').val();
            var status = $('#status').val();
            var category_id = $('#category_id').val();
            dataTable.reload({
                where: {
                    ad_title: ad_title,
                    status: status,
                    category_id: category_id
                },
                page: {curr: 1}
            });
        });

        $('#refresh').click(function () {
            var ad_title = $('#ad_title').val();
            var status = $('#status').val();
            var category_id = $('#category_id').val();
            dataTable.reload({
                where: {
                    ad_title: ad_title,
                    status: status,
                    category_id: category_id
                },
                page: {curr: 1}
            });
        });
        //头工具栏事件
        table.on('toolbar(dataTable)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            let myArray=[];
            if(data == ''){
                layer.msg('选中内容不能为空'); return false;
            }
            $.each(data, function(i,item) {
                myArray.push(item.id);
            });
            switch(obj.event){
                case 'getDisable':
                    var type = 1;
                    var content = "确定禁用吗？";
                    break;
                case 'getfrozen':
                    var type = 2;
                    var content = "确定启用吗？";
                    break;
                case 'deleteAll':
                    var type = 3;
                    var content = "确定删除吗？";
                    break;
            };
            layer.confirm(content, {icon: 3, title:'提示'}, function(index) {
                $.post("{{route('admin.ad.delete.all')}}", {type: type, myArray: myArray}, function (data) {
                    layer.msg(data.msg, {time: 2000}, function () {
                        window.location.href = document.location;
                    });
                }, "json");
            });
        });

        //拉黑
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            var event = obj.event;
            if(event === 'delete'){
                layer.confirm('删除后，将永久无法恢复，确定删除吗？', function (index) {
                    $.post("{{ route('admin.ad.delete') }}", {_method: 'delete', id: data.id}, function (result) {
                        if (result.code == 0) {
                            dataTable.reload()
                        }
                        layer.close(index);
                        layer.msg(result.msg);
                    });
                });
            }else if (event === 'imgClick') {
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    area: ['auto'],
                    skin: 'layui-layer-nobg',
                    shadeClose: true,
                    content: '<img src="'+data.thumb+'">'
                });
            }
        });

    });

</script>